import request from '@/utils/request';
import {AxiosPromise} from 'axios';
import {CreateRepoCompRequestVO, RepoCompPageResult, RepoCompQueryParam,} from './types';
import {FileInfo} from "@/api/cloud/repository/fileupload/types";

export function listRepoCompEntityPages(queryParams: RepoCompQueryParam): AxiosPromise<RepoCompPageResult> {
    return request({
        url: '/manager/repository/component/haas/page',
        method: 'post',
        data: queryParams,
    });
}

export function createRepoCompEntity(data: CreateRepoCompRequestVO) {
    return request({
        url: '/manager/repository/component/haas/entity',
        method: 'post',
        data: data,
    });
}

export function updateRepoCompEntity(data: CreateRepoCompRequestVO) {
    return request({
        url: '/manager/repository/component/haas/entity',
        method: 'put',
        data: data,
    });
}

export function deleteRepoCompEntity(data: CreateRepoCompRequestVO) {
    return request({
        url: '/manager/repository/component/haas/entity',
        method: 'delete',
        params: {
            id: data.id,
        },
    });
}

export function updateRepoCompCommitKey(data: CreateRepoCompRequestVO) {
    return request({
        url: '/manager/repository/component/haas/commitKey',
        method: 'put',
        data: {
            id: data.id,
            commitKey: data.commitKey,
        },
    });
}

/**
 * 上传文件
 *
 * @param file
 */
export function uploadFile(file: File, hardManufacturer: string, hardDeviceType: string, softManufacturer: string, softDeviceType: string, commitKey: string): AxiosPromise<FileInfo> {
    // 文件下载
    console.info("文件上传")

    const formData = new FormData();
    formData.append('file', file);
    formData.append('hardManufacturer', hardManufacturer);
    formData.append('hardDeviceType', hardDeviceType);
    formData.append('softManufacturer', softManufacturer);
    formData.append('softDeviceType', softDeviceType);
    formData.append('commitKey', commitKey);
    return request({
        url: '/manager/repository/component/haas/upload', method: 'post', data: formData, headers: {
            'Content-Type': 'multipart/form-data'
        }
    });
}

export function downloadFiles(fileName: any, url: any): any {
    // 文件下载
    console.info("文件下载")
    const link = document.createElement("a");
    link.href = url;
    link.download = fileName; // 文件名称需要自己定义。如果需要知道也可以在header中自定义一个文件名称返回 也可以随机生成一个 我这里是已经提前知道了文件名
    link.target = "_blank";
    link.click();
}

